package keshe;

public class All extends SparseMatrix{
    public All(int[][] mat) {
        super(mat);
    }
    public All(int maxSize) {
        super(maxSize);
    }

    public void printMaterix () {
        int i;
        System.out.println("稀疏矩阵的三元组存储结构为：");
        System.out.println("行数：" + rows + ",列数：" + cols + ",非零元素个数：" + nums);

        for (i = 0; i < nums; i++) {
            System.out.println(data[i].row + "\t" + data[i].column + "\t" + data[i].value);
        }

    }//三元组表的打印
//            public void All() {
//             int[][] n = new int[rows][cols];
//              for (int i= 0; i< nums; i++) {
//               n[data[i].row][data[i].column] = data[i].value;
//                }
//                System.out.println("稀疏矩阵为：");
//                for (int a = 0; a < rows; a++) {
//                    for (int b = 0; b < cols; b++) {
//                   System.out.print(n[a][b] + "  ");
//                    }
//
//                    System.out.println("");
//                }
//           }//用三元组表输出矩阵



    public All fasttranspose(){
        All tm=new All(nums);
        tm.cols=rows;
        tm.rows=cols;
        tm.nums=nums;
        int i,j=0,k=0;
        int[] num,copt;
        if(nums>0){
            num=new int[rows];
            copt=new int[rows];
            for (i=0;i<cols;i++){
                num[i]=0;
            }
            for (i=0;i<nums;i++){
                j=data[i].column;
                num[j]++;
            }
            copt[0]=0;
            for (i=1;i<cols;i++){
                copt[i]=copt[i-1]+num[i-1];
            }//执行转置操作
            for (i=0;i<nums;i++){
                j=data[i].column;
                k=copt[j];
                tm.data[k].row=data[i].column;
                tm.data[k].column=data[i].row;
                tm.data[k].value=data[i].value;
                copt[j]++;
            }
        }
        return tm;
    }//矩阵的快速转置
    public void output0() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }
    }//用三元组表输出矩阵
    public void output1() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("转置后稀疏矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }//矩阵转置的打印
    }

public static All Add(All a, All b) {
        if (a.rows != b.rows || a.cols != b.cols) {
            System.out.println("两矩阵不能相加");
            return null;

        }

        System.out.println("矩阵可以相加，矩阵相加后为：");
        All c = new All(a.nums + b.nums);
        int i = 0, j = 0, k = 0;
        int len = 0;
         c.rows = a.rows;
         c.cols = b.cols;
         while (i < a.nums && j < b.nums) {
            if (a.data[i].row < b.data[j].row) {
                c.data[k].column = a.data[i].column;
                c.data[k].row = a.data[i].row;
                c.data[k].value = a.data[i].value;
                c.nums = ++k;
                i++;
            } else if (a.data[i].row == b.data[j].row) {
                if (a.data[i].column == b.data[j].column) {
                    if (a.data[i].value + b.data[j].value != 0) {
                        c.data[k].column = a.data[i].column;
                        c.data[k].row = a.data[i].row;
                        c.data[k].value = a.data[i].value + b.data[j].value;
                        c.nums = ++k;

                    }
                    i++;
                    j++;
                } else if (a.data[i].column < b.data[j].column) {
                    c.data[k].column = a.data[i].column;
                    c.data[k].row = a.data[i].row;
                    c.data[k].value = a.data[i].value;
                    c.nums = ++k;
                    i++;
                } else if (a.data[i].column > b.data[j].column) {
                    c.data[k].column = b.data[j].column;
                    c.data[k].row = b.data[j].row;
                    c.data[k].value = b.data[j].value;
                    c.nums = ++k;
                    j++;
                } else if (a.data[i].row > b.data[j].row) {
                    c.data[k].column = b.data[j].column;
                    c.data[k].row = b.data[j].row;
                    c.data[k].value = b.data[j].value;
                    c.nums = ++k;
                    j++;
                }
            }
            while (i < a.nums) {
                c.data[k].column = a.data[i].column;
                c.data[k].row = a.data[i].row;
                c.data[k].value = a.data[i].value;
                c.nums = ++k;
                i++;
            }
            while (j < b.nums) {
                c.data[k].column = b.data[j].column;
                c.data[k].row = b.data[j].row;
                c.data[k].value = b.data[j].value;
                c.nums = ++k;
                j++;
            }

        }
         c.output0();
         c.printMaterix();

        return c;
    }
    public static All Multiple(All a, All b) {
        int i = 0, j = 0, k = 0;
        int len = 0;
        All c = new All(a.nums + b.nums);
        c.rows = a.rows;
        c.cols = b.cols;
        if (a.cols != b.rows ) {
            System.out.println("两矩阵不能相乘");
            return null;

        }
        else  {
            System.out.println("矩阵可以相乘，矩阵相乘后为：");
                while (i<a.nums){
                    for(j=0;j<b.nums;j++){

                        if(a.data[i].column==b.data[j].row){
                            c.data[k].row=a.data[i].row;
                            c.data[k].column=b.data[j].column;
                            c.data[k].value=a.data[i].value*b.data[j].value;
                            for (len=0;len<k;len++){
                                if(c.data[k].row==c.data[len].row&&c.data[k].column==c.data[len].column){
                                    c.data[len].value+=c.data[k].value;
                                    k--;
                                }

                            }
                            k++;
                        }

                    }i++;
        }c.nums=k;

        }
        c.output0();
        c.printMaterix();
        return c;
        }



    }

